﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using XpCtrl;
using System.Drawing;

public partial class Admin_HrManage : System.Web.UI.Page
{
    HR hrM;
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.Title = "云南希普防雷工程有限公司";
            SourceBind();
        }
    }

    protected void SourceBind()
    {
        String strConn = ConfigurationManager.ConnectionStrings["xipuConnectionString"].ToString();
        hrM = new HR(strConn);

        jobList.AllowPaging = true;
        jobList.PageSize = 15;
        jobList.Width = 395;
        jobList.BorderStyle = BorderStyle.Double;
        jobList.AutoGenerateDeleteButton = false;

        ds = hrM.GetJobList();
        jobList.DataSource = ds;
        jobList.DataBind();

        LabTatalNo.Text = ds.Tables[0].Rows.Count.ToString();
        int buffer = jobList.PageIndex * 15 + 1;
        LabCurrentStart.Text = buffer.ToString();
        if (jobList.PageIndex * 15 + 15 >= ds.Tables[0].Rows.Count)
        {
            LabCurrentEnd.Text = ds.Tables[0].Rows.Count.ToString();
        }
        else
        {
            buffer = jobList.PageIndex * 15 + 15;
            LabCurrentEnd.Text = buffer.ToString();
        }
    }

    protected void jobList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        jobList.PageIndex = e.NewPageIndex;

        SourceBind();

    }

    protected void jobList_RowCreated(object sender, GridViewRowEventArgs e)
    {
        #region 翻页绑定
        if (e.Row.RowType == DataControlRowType.Pager)
        {
            Label label_Index = new Label();
            LinkButton Button_IndexFirst = new LinkButton();
            LinkButton Button_IndexLast = new LinkButton();
            LinkButton Button_IndexNext = new LinkButton();
            LinkButton Button_IndexPrevious = new LinkButton();

            Button_IndexFirst.Text = "第一页 ";
            Button_IndexFirst.Font.Size = 10;
            Button_IndexFirst.Font.Name = "黑体";
            Button_IndexFirst.CommandName = "first";
            Button_IndexFirst.ForeColor = Color.Black;
            Button_IndexFirst.Click += new EventHandler(PageButtonClick);

            Button_IndexNext.Text = "   下一页 ";
            Button_IndexNext.Font.Size = 10;
            Button_IndexNext.Font.Name = "黑体";
            Button_IndexNext.CommandName = "next";
            Button_IndexNext.ForeColor = Color.Black;

            Button_IndexNext.Click += new EventHandler(PageButtonClick);

            Button_IndexPrevious.Text = "前一页 ";
            Button_IndexPrevious.Font.Size = 10;
            Button_IndexPrevious.Font.Name = "黑体";
            Button_IndexPrevious.CommandName = "previous";
            Button_IndexPrevious.ForeColor = Color.Black;
            Button_IndexPrevious.Click += new EventHandler(PageButtonClick);

            Button_IndexLast.Text = "最末页 ";
            Button_IndexLast.Font.Size = 10;
            Button_IndexLast.Font.Name = "黑体";
            Button_IndexLast.CommandName = "last";
            Button_IndexLast.ForeColor = Color.Black;
            Button_IndexLast.Click += new EventHandler(PageButtonClick);

            label_Index.Text = "当前为第" + (jobList.PageIndex + 1) + "页,共有" + ((GridView)sender).PageCount + "页";
            label_Index.Font.Size = 8;
            e.Row.Controls[0].Controls[0].Controls[0].Controls[0].Controls.AddAt(0, (Button_IndexFirst));
            e.Row.Controls[0].Controls[0].Controls[0].Controls[0].Controls.AddAt(1, (Button_IndexPrevious));

            int controlTmp = e.Row.Controls[0].Controls[0].Controls[0].Controls.Count - 1;
            e.Row.Controls[0].Controls[0].Controls[0].Controls[controlTmp].Controls.Add(Button_IndexNext);
            e.Row.Controls[0].Controls[0].Controls[0].Controls[controlTmp].Controls.Add(Button_IndexLast);

            e.Row.Controls[0].Controls[0].Controls[0].Controls[controlTmp].Controls.Add(label_Index);

            //e.Row.Controls[0].Controls.Add(label_Index);
        }
        #endregion
    }


    protected void PageButtonClick(object sender, EventArgs e)
    {
        LinkButton clickedButton = ((LinkButton)sender);
        if (clickedButton.CommandName == "first")
        {
            jobList.PageIndex = 0;
        }
        else if (clickedButton.CommandName == "next")
        {
            if (jobList.PageIndex < jobList.PageCount - 1)
            {
                jobList.PageIndex += 1;
            }
        }
        else if (clickedButton.CommandName == "previous")
        {
            if (jobList.PageIndex >= 1)
            {
                jobList.PageIndex -= 1;
            }
        }
        else if (clickedButton.CommandName == "last")
        {
            jobList.PageIndex = jobList.PageCount - 1;
        }
        SourceBind();
    }

    protected void jobList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "deleteJob")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            String strConn = ConfigurationManager.ConnectionStrings["xipuConnectionString"].ToString();
            hrM = new HR(strConn);

            int n = hrM.DeleteJob(index);

            SourceBind();
            if (n == 0)
            {
                Response.Write("<script>alert('删除失败，请重新操作！');</script>");
            }
        }

        else if (e.CommandName == "editJob")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            Response.Redirect("editJob.aspx?ID="+index);
        }

        else if (e.CommandName == "showJob") 
        {
            int index = Convert.ToInt32(e.CommandArgument);
            Response.Redirect("jobDetail.aspx?ID=" + index);
        }
    }
}
